using System;
using System.Windows.Forms;
using MediaPortal.GUI.Library;
using MediaPortal.GUI.Video;
//using MediaPortal.Video.Database;
using MediaPortal.Util;
using MediaPortal.Dialogs;
using MediaPortal.Player;
using MediaPortal.Configuration;
using System.Data;
using System.ComponentModel;
using c3o.Framework.Web;
using System.Collections.Specialized;
using System.Xml;
using System.Xml.Serialization;
using System.IO;


namespace MediaPortal.GUI.Rss
{
	public class GUIPodCasts : GUIRssBaseWindow, ISetupForm
	{
        public static bool useVMR9 = true;

		public override int GetID
		{
			get
			{
				return 61902;
			}
			set
			{
			}
		}

        protected override void OnShowContextMenu()
        {
            GUIDialogMenu dlg = (GUIDialogMenu)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_MENU);

            if (dlg == null)
                return;

            dlg.Reset();
            dlg.SetHeading(924);                // Menu
            dlg.AddLocalizedString(335);		// Start
            dlg.AddLocalizedString(1032);		// Stop
            dlg.Add("Start");
            dlg.Add("Stop");
            dlg.Add("Move Up");
            dlg.Add("Move Down");
            dlg.Add("Remove");

            dlg.DoModal(GetID);

         
        }



		protected override void OnPageLoad()
		{
			this.xmlSection = "Podcasts";
			this.RssFileName = "MyPodCasts.Feeds.opml.xml";
			this.OpmlFileName = "MyPodCasts.Search.opml.xml";

			GUIPropertyManager.SetProperty("#header.label", "My PodCasts");

			base.OnPageLoad();

            using (MediaPortal.Profile.Settings xmlreader = new MediaPortal.Profile.Settings(Config.GetFile(Config.Dir.Config, "MediaPortal.xml")))
            {
                useVMR9 = xmlreader.GetValueAsBool("general", "usevrm9forwebstreams", true);
            }
		}


		public override bool Init()
		{
			//this.RssFileName = "MyPodCasts.Feeds.opml.xml";
			//this.OpmlFileName = "MyPodCasts.Search.opml.xml";

			//g_Player.PlayBackStopped += new MediaPortal.Player.g_Player.StoppedHandler(OnPlayBackStopped);
			//g_Player.PlayBackEnded += new MediaPortal.Player.g_Player.EndedHandler(OnPlayBackEnded);
			//g_Player.PlayBackStarted += new MediaPortal.Player.g_Player.StartedHandler(OnPlayBackStarted);
	
			return Load(GUIGraphicsContext.Skin + @"\MyPodCasts.xml");
		}



		#region ISetupForm Members

		// Returns the name of the plugin which is shown in the plugin menu
		public string PluginName()
		{
			return "My PodCasts";
		}



		// Returns the description of the plugin is shown in the plugin menu
		public string Description()
		{
			return "My PodCasts";
		}



		// Returns the author of the plugin which is shown in the plugin menu
		public string Author()
		{
			return "fcsobel";
		}



		// show the setup dialog
		public void ShowPlugin()
		{
			Form setup = new MyPodCastSetupForm();
			setup.ShowDialog();
		}



		// Indicates whether plugin can be enabled/disabled
		public bool CanEnable()
		{
			return true;
		}

		// get ID of windowplugin belonging to this setup
		public int GetWindowId()
		{
			return this.GetID;
		}

		// Indicates if plugin is enabled by default;
		public bool DefaultEnabled()
		{
			return true;
		}

		// indicates if a plugin has its own setup screen
		public bool HasSetup()
		{
			return true;
		}


		/// <summary>
		/// If the plugin should have its own button on the main menu of Media Portal then it
		/// should return true to this method, otherwise if it should not be on home
		/// it should return false
		/// </summary>
		/// <param name="strButtonText">text the button should have</param>
		/// <param name="strButtonImage">image for the button, or empty for default</param>
		/// <param name="strButtonImageFocus">image for the button, or empty for default</param>
		/// <param name="strPictureImage">subpicture for the button or empty for none</param>
		/// <returns>true  : plugin needs its own button on home
		///          false : plugin does not need its own button on home</returns>
		//public bool GetHome(out string strButtonText, out string strButtonImage, out string strButtonImageFocus, out string strPictureImage)
		//{

		//    strButtonText = "UKNova";
		//    strButtonImage = String.Empty;
		//    strButtonImageFocus = String.Empty;
		//    strPictureImage = String.Empty;
		//    return false;
		//}

		public bool GetHome(out string strButtonText, out string strButtonImage, out string strButtonImageFocus, out string strPictureImage)
		{
			strButtonText = PluginName();
			strButtonImage = String.Empty;
			strButtonImageFocus = String.Empty;
			strPictureImage = String.Empty;
			return true;
		}
		#endregion


		//protected override void OnClicked(int controlId, GUIControl control, Action.ActionType actionType)
		//{
		//    //base.OnClicked(controlId, control, actionType);
		//    //if (control == this.bttnAddSite) this.ShowOpmlSites();
		//    //if (control == this.facadeView)     this.OnListView();
		//    //if (control == this.bttnAddSearchTag) this.AddSearchTag();
		//    //if (control == this.bttnDelete) this.OnDelete();

		//    base.OnClicked(controlId, control, actionType);
		//}



		public override void OnAction(Action action)
		{
			base.OnAction(action);
		}


		protected override void OnListViewList()
		{
			base.OnListViewList();

			GUIListItem item = this.facadeView.SelectedListItem;

			if (item != null)
			{
				if (item.IsFolder)
				{
					if (item.Label == "..")
					{
						if (this.selectedOutline == null)
						{
							this.ShowSites();
						}
						else
						{
							this.SelectOutline(this.opmlList, this.selectedOutline.parent, this.DefaultView);
						}
					}
				}
				else
				{
					// Select Item
					item.IconImage = "check-box.png";
					item.IsDownloading = true;

					// Refresh list so you see change
					GUIControl.RefreshControl(this.GetID, this.facadeView.GetID);

					// Download File
					string url = this.facadeView.SelectedListItem.Path;

					if (url != null && url.Length > 0)
					{
                        //this.PlayVideo(url);
                        this.PlayIt(url, this.facadeView.SelectedListItem.Label);

                       //// PlayUrl(url, this.facadeView.SelectedListItem.Label);
                       // if (url.Contains(".mp3") ||
                       //     url.Contains(".wav") ||
                       //     url.Contains(".wma") ||
                       //     url.Contains(".aac"))
                       // {
                       //     if (g_Player.PlayAudioStream(url))
                       //     {
                       //         //g_Player.FullScreen = false;
                       //     }
                       //     else
                       //     {
                       //         Log.Info("GUIPodCasts: Unable to play {0}", url);
                       //         GUIDialogNotify dlg = (GUIDialogNotify)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_NOTIFY);
                       //         dlg.SetHeading("ERROR");
                       //         dlg.SetText("Unable to play the selected stream. Please try again later.");
                       //         dlg.DoModal(GUIWindowManager.ActiveWindow);
                       //     }
                       // }
                       // else
                       // {
                       //     if (g_Player.Play(url))
                       //     {
                       //         GUIGraphicsContext.IsFullScreenVideo = true;
                       //         GUIWindowManager.ActivateWindow((int)GUIWindow.Window.WINDOW_FULLSCREEN_VIDEO);
                       //         g_Player.FullScreen = true;

                       //         //g_Player.ShowFullScreenWindow();
                       //         //g_Player.FullScreen = true;
                       //     }
                       //     else
                       //     {
                       //         Log.Info("GUIPodCasts: Unable to play {0}", url);
                       //         GUIDialogNotify dlg = (GUIDialogNotify)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_NOTIFY);
                       //         dlg.SetHeading("ERROR");
                       //         dlg.SetText("Unable to play the selected stream. Please try again later.");
                       //         dlg.DoModal(GUIWindowManager.ActiveWindow);
                       //     }
                       // }
                        
					}
				}
			}
		}

//        void PlayItOld(string url)
//        {
//            if (url.ToLower().StartsWith("mms:"))
//            {
//                if (g_Player.PlayVideoStream(url))
//                {
////                    g_Player.ShowFullScreenWindow();
//                    GUIGraphicsContext.IsFullScreenVideo = true;
//                    GUIWindowManager.ActivateWindow((int)GUIWindow.Window.WINDOW_FULLSCREEN_VIDEO);
//                    g_Player.FullScreen = true;
//                }
//                else
//                {
//                    Log.Info("GUIPodCasts: Unable to play {0}", url);
//                    GUIDialogNotify dlg = (GUIDialogNotify)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_NOTIFY);
//                    dlg.SetHeading("ERROR");
//                    dlg.SetText("Unable to play the video stream. Please try again later.");
//                    dlg.DoModal(GUIWindowManager.ActiveWindow);
//                }
//            }

//            if (g_Player.Play(url))
//            {
//                url = url.ToLower();

//                if (url.Contains(".mp3") ||
//                    url.Contains(".wav") ||
//                    url.Contains(".wma") ||
//                    url.Contains(".aac"))
//                {
//                    // Playing audio only
//                }
//                else
//                {
//                    // Play video full screen
//                    GUIGraphicsContext.IsFullScreenVideo = true;
//                    GUIWindowManager.ActivateWindow((int)GUIWindow.Window.WINDOW_FULLSCREEN_VIDEO);
//                    g_Player.FullScreen = true;

//                    //g_Player.ShowFullScreenWindow();
//                    //g_Player.FullScreen = true;
//                }
//            }
//            else
//            {
//                Log.Info("GUIPodCasts: Unable to play {0}", url);
//                GUIDialogNotify dlg = (GUIDialogNotify)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_NOTIFY);
//                dlg.SetHeading("ERROR");
//                dlg.SetText("Unable to play the stream. Please try again later.");
//                dlg.DoModal(GUIWindowManager.ActiveWindow);
//            }
//        }


        void PlayIt(string url, string name)
        {
            if (url.ToLower().StartsWith("mms:"))
            {
                if (g_Player.PlayVideoStream(url, name))
                {
                    g_Player.ShowFullScreenWindow();
                    g_Player.FullScreen = true;
                }
                else
                {
                    Log.Info("GUIPodCasts: Unable to play {0}", url);
                    GUIDialogNotify dlg = (GUIDialogNotify)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_NOTIFY);
                    dlg.SetHeading("ERROR");
                    dlg.SetText("Unable to play the video stream.");
                    dlg.DoModal(GUIWindowManager.ActiveWindow);
                }
            }

            if (url.Contains(".mp3") ||
                url.Contains(".wav") ||
                url.Contains(".wma") ||
                url.Contains(".aac"))
            {
                if (!g_Player.PlayAudioStream(url))
                {
                    Log.Info("GUIPodCasts: Unable to play {0}", url);
                    GUIDialogNotify dlg = (GUIDialogNotify)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_NOTIFY);
                    dlg.SetHeading("ERROR");
                    dlg.SetText("Unable to play the audio stream.");
                    dlg.DoModal(GUIWindowManager.ActiveWindow);
                }
            }
            else
            {
                if (g_Player.Play(url))
                {
                    // Play video full screen
                    g_Player.ShowFullScreenWindow();
                    g_Player.FullScreen = true;
                }
                else
                {
                    Log.Info("GUIPodCasts: Unable to play {0}", url);
                    GUIDialogNotify dlg = (GUIDialogNotify)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_NOTIFY);
                    dlg.SetHeading("ERROR");
                    dlg.SetText("Unable to play the video.");
                    dlg.DoModal(GUIWindowManager.ActiveWindow);
                }

            }
        }

        //void PlayVideoOld(string url)
        //{
        //    if (url.ToLower().Contains(".avi") ||
        //        url.ToLower().Contains(".mpeg") ||
        //        url.ToLower().Contains(".mov") ||
        //        url.ToLower().Contains(".wmv") ||
        //        url.ToLower().Contains(".divx") ||
        //        url.ToLower().Contains(".mpg"))
        //    {
        //        if (g_Player.Play(url))
        //        {
        //            //GUIGraphicsContext.IsFullScreenVideo = true;
        //            //GUIWindowManager.ActivateWindow((int)GUIWindow.Window.WINDOW_FULLSCREEN_VIDEO);
        //            //g_Player.FullScreen = true;

        //            g_Player.ShowFullScreenWindow();
        //            g_Player.FullScreen = true;
        //        }
        //        else
        //        {
        //            Log.Info("GUIPodCasts: Unable to play {0}", url);
        //            GUIDialogNotify dlg = (GUIDialogNotify)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_NOTIFY);
        //            dlg.SetHeading("ERROR");
        //            dlg.SetText("Unable to play the selected stream. Please try again later.");
        //            dlg.DoModal(GUIWindowManager.ActiveWindow);
        //        }
        //    }
        //    else
        //    {
        //        if (g_Player.PlayAudioStream(url))
        //        {
        //            g_Player.FullScreen = false;
        //        }
        //        else
        //        {
        //            Log.Info("GUIPodCasts: Unable to play {0}", url);
        //            GUIDialogNotify dlg = (GUIDialogNotify)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_NOTIFY);
        //            dlg.SetHeading("ERROR");
        //            dlg.SetText("Unable to play the selected stream. Please try again later.");
        //            dlg.DoModal(GUIWindowManager.ActiveWindow);
        //        }
        //    }
        //}


        //void playVideo(string url)
        //{
        //    if (useVMR9)
        //    {
        //        g_Player.PlayVideoStream(url, "test");
        //        g_Player.ShowFullScreenWindow(); 
        //    }
        //    else
        //    {
        //        g_Player.PlayAudioStream(url);
        //    }

        //    //if (g_Player.Playing)
        //    //{                
        //    //    g_Player.ShowFullScreenWindow();                
        //    //}
        //    //else
        //    //{
        //    //    Log.Info("GUIPodCasts: Unable to play {0}", url);
        //    //    GUIDialogNotify dlg = (GUIDialogNotify)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_NOTIFY);
        //    //    dlg.SetHeading("ERROR");
        //    //    dlg.SetText("useVMR9 Unable to play this stream. Please try again later.");
        //    //    dlg.DoModal(GUIWindowManager.ActiveWindow);
        //    //}
        //}

        //public void PlayUrl(string url, string videoName)
        //{
        //    //GetGUIProperties();
        //    //MMSUrl = string.Empty;
        //    //GetMMSURL(url);
        //    //Log.Debug("Url: {0}, mmsurl: {1}, videoname: {2}", url, MMSUrl, videoName);
            
        //    bool playOk = false;

        //    if (useVMR9)
        //    {
        //        playOk = g_Player.PlayVideoStream(url, videoName);
        //    }
        //    else
        //    {
        //        playOk = g_Player.PlayAudioStream(url);
        //        //playOk = g_Player.PlayAudioStream(url, true);
        //    }

        //    if (playOk)
        //    {
        //        g_Player.ShowFullScreenWindow();
        //        g_Player.FullScreen = true;
        //    }
        //    else
        //    {
        //        Log.Info("GUIPodCasts: Unable to play {0}", url);
        //        GUIDialogNotify dlg = (GUIDialogNotify)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_NOTIFY);
        //        dlg.SetHeading("ERROR");
        //        dlg.SetText("Unable to play the selected stream. Please try again later.");
        //        dlg.DoModal(GUIWindowManager.ActiveWindow);
        //    }
        //}



	}
}